package yi.wuhenbot.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yi.wuhenbot.dao.FishingShopMapper;
import yi.wuhenbot.dao.OrgUserServiceMapper;
import yi.wuhenbot.entity.Fishing.FishingShop;
import yi.wuhenbot.Controller.BotController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;


/**
 * @description 钓鱼商品服务类
 * @author wuhen
 */
@Service("fishingShopService")
public class FishingShopService {
    @Autowired
    private FishingShopMapper fishingShopMapper;

    @Autowired
    private OrgUserServiceMapper orgUserServiceMapper;
    @Autowired
    private FishingUserService fishingUserService;
    private static final Logger logger = LoggerFactory.getLogger(BotController.class);


    String content="";

    /**
     * 查询全部钓鱼商品
     * @return 钓鱼商品列表
     */
    public String selectAll(String typeId,String type) {
        // 将数据解析出来并以文字展示
        content="";
        List<FishingShop> fishingShops = fishingShopMapper.selectAll();
        for (FishingShop fishingShop : fishingShops) {
            //美化输出：FishingShop(shop_id=1, equipment_id=123, equipment_url=0, listed_time=2025-09-09 14:05:59)，时间移除2025年付以及秒

            content += "序号:"+fishingShop.getShop_id() +" || 名称:" + fishingShop.getEquipment_name() + " || 价格:" + fishingShop.getPrice()+"\n";
        }
        // 移除最后的换行符
        if (content.endsWith("\n")) {
            content = content.substring(0, content.length() - 1);
        }
        return content;
    }
    /**
     * 根据商品名称查询商品价格
     * @return 商品价格
     */
    public int getPirceByName(String typeId,String type,String name) {
        return fishingShopMapper.getPirceByName(name);
    }
    /**
     * 购买商品
     * 1.判断用户是否存在
     * 2.判断商品是否存在
     * 3.判断用户是否有足够的积分
     * 4.购买商品，扣除积分，更新用户信息
     * 5.返回购买结果
     * @param userOpenid 用户openid
     * @param name 商品名称
     * @param num 购买数量
     * @return 购买结果
     */
    public String buyFishingLine(String typeId,String type,String userOpenid, String name,String num) {
        // 将字符串num转化为int


        int numInt = Integer.parseInt(num);

        //参数打印
        logger.info("购买商品参数：userOpenid={},name={},num={}",userOpenid,name,num);

        // 1.判断用户是否存在
        if (!orgUserServiceMapper.isUserExist(userOpenid)){
            return "用户不存在";
        }
        // 2.判断商品是否存在
        if (!fishingShopMapper.isExistByName(name)){
            return "商品不存在,请检查商品名称";
        }
        // 3.判断用户是否有足够的积分
        int points = orgUserServiceMapper.getPoints1(userOpenid);
        int price = fishingShopMapper.getPirceByName(name);
        if (points < price*numInt){
            return "积分不足"+"需要"+price*numInt+"积分,您当前有"+points+"积分";
        }
        // 4.购买商品，扣除积分，更新用户信息
        orgUserServiceMapper.setPoint1(userOpenid,points-price*numInt);
        // 更新钓鱼信息增加鱼饵数量
        int lineNum = fishingUserService.updateFishingLine(userOpenid,price,numInt);
        if (lineNum == 0){
            return "购买失败";
        }
        // 5.返回购买结果
        return "购买成功"+"增加了"+numInt+"条鱼饵";
    }

}
